package com.wpx.log.core.before;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.wpx.log.core.LogContext;
import com.wpx.log.core.LogRequestContext;

/**
 * 处理ip 黑白名单
 */
@Component
public class IpFilterIBefore implements IBefore {
	
	private static final Logger logger = LoggerFactory.getLogger(IpFilterIBefore.class);
			
	@Override
	public void execute(LogContext logContext, LogRequestContext requestContext, ProceedingJoinPoint point) {
		String ip = requestContext.getIp();
		String group = requestContext.getOpLogAnnotationAsMethod().group();
		
		if(logContext.getProperties().isDebug()) {
			logger.info("{} 是否是白名单：{}", ip, logContext.isWhiteIp(group, ip));
			logger.info("{} 是否是黑名单：{}", ip, logContext.isBlackIp(group, ip));
		}
						
		requestContext.setIpPass(logContext.isWhiteIp(group, ip) && !logContext.isBlackIp(group, ip));
	}

}
